home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / strpp300.zip / STRNG.H < prev    next >
C/C++ Source or Header  |  1993-04-11  |  14KB  |  402 lines

  1. /* -------------------------------------------------------------------- */
  2. /* String++ Version 3.00                                       04/10/93 */
  3. /*                                                                      */
  4. /* Enhanced string class for Turbo C++/Borland C++.                     */
  5. /* Copyright 1991-1993 by Carl W. Moreland                              */
  6. /*                                                                      */
  7. /* strng.h                                                              */
  8. /* -------------------------------------------------------------------- */
  9.  
  10. #ifndef _STRNG_H
  11. #define _STRNG_H
  12.  
  13. #ifndef __CLSTYPES_H
  14. #include <ClsTypes.h>
  15. #endif
  16.  
  17. #ifndef __SORTABLE_H
  18. #include <Sortable.h>
  19. #endif
  20.  
  21. #ifndef __STRING_H
  22. #include <string.h>
  23. #endif
  24.  
  25. const char LEFT   = 0;
  26. const char CENTER = 1;
  27. const char RIGHT  = 2;
  28. const char NOCLIP = 0;
  29. const char NOTRIM = 0;
  30. const char CLIP   = 1;
  31. const char TRIM   = 2;
  32. const char WHITESPACE = 0;
  33.  
  34. #pragma option -Vo-
  35. #if defined( __BCOPT__ ) && !defined( _ALLOW_po )
  36. #pragma option -po-
  37. #endif
  38.  
  39. _CLASSDEF(ostream)
  40. _CLASSDEF(RegExp)
  41. _CLASSDEF(String)
  42.  
  43. class _CLASSTYPE String : public Sortable
  44. {
  45. protected:
  46.   unsigned strLen;            // length of the String
  47.   char*    strPtr;            // pointer to the String contents
  48.   unsigned bufferLen;            // length of the buffer
  49.  
  50. public:
  51.   String();                // default constructor;
  52.   String(const char c,            // initialize with a character,
  53.          const unsigned n = 1);        //   optional # of characters
  54.   String(const char* p);        // initialize with a char *
  55.   String(const char* p,            // initialize with a char *,
  56.          const unsigned pos,        //   optional starting char
  57.          const unsigned len = 10000);    //   optional # of chars
  58.   String(const String& s);        // initialize with another String
  59.   String(const String& s,        // initialize with another String,
  60.          const unsigned pos,        //   optional starting char
  61.          const unsigned len = 10000);    //   optional # of chars
  62.   String(const int n);            // initialize with an integer
  63.   String(const long n);            // initialize with a long int
  64.   String(const float n,            // initialize with a float
  65.          const char* format = "");    //   and a format specifier
  66.   String(const double n,        // initialize with a double
  67.          const char* format = "");    //   and a format specifier
  68.  
  69.  ~String(void);
  70.  
  71. protected:
  72.   static unsigned strMinLength;        // minimum memory allocated
  73.   static unsigned strIncLength;        // incremental memory allocated
  74.   static String*  findIn;        // these are for FindFirst, etc.
  75.   static String&  findStr;
  76.   static unsigned findPos;
  77.   static String   fpFormat;
  78.  
  79.   virtual void SetStr(const char c,
  80.                       const unsigned n = 1);
  81.   virtual void SetStr(const char* p);
  82.   virtual void SetStr(const char* p,
  83.                       const unsigned pos,
  84.               const unsigned len = 10000);
  85.   virtual void SetStr(const String& s);
  86.   virtual void SetStr(const String& s,
  87.                       const unsigned pos,
  88.               const unsigned len = 10000);
  89.   virtual void AddStr(const char c);
  90.   virtual void AddStr(const char* p);
  91.   virtual void AddStr(const String& s);
  92.   virtual unsigned GetSize(unsigned n);
  93.   void ltos(const long n);
  94.   void dtos(const double n, const char* format);
  95.  
  96. public:
  97.   void SetMinLength(unsigned len = 16) const { strMinLength = len; }
  98.   void SetIncLength(unsigned len = 8)  const { strIncLength = len; }
  99.   unsigned SetSize(unsigned len);
  100.   void Minimize(void);                // minimize bufferLen
  101.   void SetFloatFormat(const char*) const;     // floating point format
  102.  
  103.   virtual operator const char() const        { return strPtr[0]; }
  104.   virtual operator const char*() const       { return strPtr; }
  105.   virtual operator char*() const             { return strPtr; }
  106.   virtual const char  operator *() const     { return strPtr[0]; }
  107.   virtual const char* operator()() const     { return strPtr; }
  108.   const char* operator()(unsigned pos) const { return strPtr+pos; }
  109.   String      operator()(unsigned pos, unsigned len) const;
  110.   char* ptr(void) const                      { return strPtr; }
  111.   char* ptr(unsigned pos) const              { return strPtr+pos; }
  112.  
  113.   virtual int Length(void) const { return strLen; }
  114.   virtual int Len(void)    const { return strLen; }
  115.   String& toUpper(void);            // convert to uppercase
  116.   String& toLower(void);            // convert to lowercase
  117.   int&    Value(int& n) const;            // int value of str
  118.   long&   Value(long& n) const;            // long int value of str
  119.   float&  Value(float& n) const;        // float value of str
  120.   double& Value(double& n) const;        // double value of str
  121.  
  122.   String& Left(unsigned len);            // left   len chars
  123.   String& Right(unsigned len);            // right  len chars
  124.   String& Mid(unsigned pos,            // middle len chars from pos
  125.               unsigned len);
  126.   String& Justify(char type,            // justify str
  127.                   unsigned len,
  128.           char mode = CLIP|TRIM);
  129.   String& Trim(int mode = CENTER,        // delete whitespace
  130.                char ch = WHITESPACE);
  131.  
  132.   String& Insert(unsigned pos,            // insert substring
  133.                  const String& s);
  134.   String& Delete(unsigned pos,            // delete substring
  135.                  unsigned len = 1);
  136.   String& Replace(unsigned pos,
  137.                   unsigned len,
  138.                   const String& to);
  139.   int     Replace(const String& from,        // substitute from -> to
  140.                   const String& to,
  141.               unsigned count = 10000);
  142.   int     Replace(const RegExp& from,
  143.                   const String& to,
  144.                   unsigned count = 10000);
  145.   char*   Copy(char*&) const;            // copy str to char*
  146.  
  147.   int     Index(const String& t) const;        // position of t in str
  148.   int     Index(const RegExp& t) const;        // position of t in str
  149.   String  SubStr(unsigned pos,            // substring at position pos
  150.                  unsigned len = 10000) const;
  151.   int     Split(String*& a,            // split into an array a on
  152.                 const String& fs) const;    //   field separator fs
  153.   int     Split(String*& a,            // split into an array a on
  154.                 const RegExp& fs) const;    //   field separator fs
  155.   int     Sub(const String& from,        // substitute from -> to
  156.               const String& to,
  157.           unsigned count = 10000);
  158.   int     Sub(const RegExp& from,        // substitute from -> to
  159.               const String& to,
  160.               unsigned count = 10000);
  161.  
  162.   int     FindFirst(const String& s) const;    // first occurance of s
  163.   int     FindNext (void) const;        // next occurance of s
  164.   int     FindPrev (void) const;        // previous occurance of s
  165.   int     FindLast (const String& s) const;    // last occurance of s
  166.  
  167.   virtual String& operator=(const char c);    // str1 = char
  168.   virtual String& operator=(const char* p);    // str1 = char*
  169.   virtual String& operator=(const String& s);    // str1 = str
  170.   virtual String& operator=(const int n);    // str1 = int
  171.   virtual String& operator=(const long n);    // str1 = long
  172.   virtual String& operator=(const float n);    // str1 = float
  173.   virtual String& operator=(const double n);    // str1 = double
  174.   String& operator+=(const char c);        // str1 += char
  175.   String& operator+=(const char* p);        // str1 += char*
  176.   String& operator+=(const String& s);        // str1 += str
  177.   String& operator*=(const unsigned n);        // str1 *= n
  178.   char&   operator[](const unsigned i) const;    // ch = str[i] or str[i] = ch
  179.  
  180.   friend String operator+(const String& s1, const String& s2);
  181.   friend String operator+(const String& s,  const char* p);
  182.   friend String operator+(const char* p,    const String& s);
  183.   friend String operator*(const String& s,  const unsigned n);
  184.   friend String operator*(const unsigned n, const String& s);
  185.  
  186.   virtual String& operator<<(const char c);    // s << char
  187.   virtual String& operator<<(const char* p);    // s << char*
  188.   virtual String& operator<<(const String& s);    // s << str
  189.   virtual String& operator<<(const int n);    // s << int
  190.   virtual String& operator<<(const long n);    // s << long
  191.   virtual String& operator<<(const float n);    // s << float
  192.   virtual String& operator<<(const double n);    // s << double
  193.  
  194.   virtual int isEqual(const Object _FAR &) const;
  195.   virtual int isLessThan(const Object _FAR &) const;
  196.   virtual classType isA() const { return stringClass; }
  197.   virtual char _FAR *nameOf() const { return "String"; }
  198.   virtual hashValueType hashValue() const;
  199.   virtual void printOn(ostream _FAR &) const;
  200. };
  201.  
  202. typedef String string;            // compatibility with older versions
  203.  
  204. /* ----- Awk-style functions ------------------------------------------ */
  205.  
  206. inline int length(const String& s) { return s.Len(); }
  207. int    index(const String& s, const String& t);
  208. String substr(const String& s, unsigned pos, unsigned len = 10000);
  209. int    split(const String& s, String*& a, const String& fs);
  210. int    gsub(const String& from, const String& to,
  211.             String& str, unsigned count = 10000);
  212. inline int sub(const String& from, const String& to, String& str) {
  213.   return gsub(from, to, str, 1);
  214. }
  215.  
  216. // Regular Expression versions - requires regexp.cpp
  217.  
  218. extern int index(const String& s, const RegExp& t);
  219. extern int split(const String& s, String*& a, const RegExp& fs);
  220. extern int gsub(const RegExp& from, const String& to,
  221.                 String& str, unsigned count = 10000);
  222. inline int sub(const RegExp& from, const String& to, String& str) {
  223.   return gsub(from, to, str, 1);
  224. }
  225.  
  226. /* ----- Other C-style functions -------------------------------------- */
  227.  
  228. String toupper(const String& s);
  229. String tolower(const String& s);
  230. String left(const String& s, unsigned len);
  231. String right(const String& s, unsigned len);
  232. String mid(const String& s, unsigned pos, unsigned len);
  233. String justify(const String& s, char type, unsigned len, char mode=CLIP|TRIM);
  234. String trim(const String& s, int mode = CENTER);
  235.  
  236. /* ----- Inline functions --------------------------------------------- */
  237.  
  238. inline String& String::operator=(const int n) {
  239.   operator=((long)n);
  240.   return *this;
  241. }
  242.  
  243. inline String& String::operator=(const float n) {
  244.   operator=((double)n);
  245.   return *this;
  246. }
  247.  
  248. inline String& String::operator<<(const int n) {
  249.   operator<<((long)n);
  250.   return *this;
  251. }
  252.  
  253. inline String& String::operator<<(const float n) {
  254.   operator<<((double)n);
  255.   return *this;
  256. }
  257.  
  258. inline int String::Replace(const String& from,
  259.                            const String& to, unsigned count) {
  260.   return gsub(from, to, *this, count);
  261. }
  262.  
  263. inline int String::Replace(const RegExp& from,
  264.                            const String& to, unsigned count) {
  265.   return gsub(from, to, *this, count);
  266. }
  267.  
  268. inline int operator==(const String& s1, const String& s2) {
  269.   return strcmp(s1, s2) == 0;
  270. }
  271.  
  272. inline int operator!=(const String& s1, const String& s2) {
  273.   return strcmp(s1, s2) != 0;
  274. }
  275.  
  276. inline int operator<(const String& s1, const String& s2) {
  277.   return strcmp(s1, s2) < 0;
  278. }
  279.  
  280. inline int operator>(const String& s1, const String& s2) {
  281.   return strcmp(s1, s2) > 0;
  282. }
  283.  
  284. inline int operator<=(const String& s1, const String& s2) {
  285.   return strcmp(s1, s2) <= 0;
  286. }
  287.  
  288. inline int operator>=(const String& s1, const String& s2) {
  289.   return strcmp(s1, s2) >= 0;
  290. }
  291.  
  292. inline int operator==(const String& s, const char* p) {
  293.   return strcmp(s, p) == 0;
  294. }
  295.  
  296. inline int operator!=(const String& s, const char* p) {
  297.   return strcmp(s, p) != 0;
  298. }
  299.  
  300. inline int operator<(const String& s, const char* p) {
  301.   return strcmp(s, p) < 0;
  302. }
  303.  
  304. inline int operator>(const String& s, const char* p) {
  305.   return strcmp(s, p) > 0;
  306. }
  307.  
  308. inline int operator<=(const String& s, const char* p) {
  309.   return strcmp(s, p) <= 0;
  310. }
  311.  
  312. inline int operator>=(const String& s, const char* p) {
  313.   return strcmp(s, p) >= 0;
  314. }
  315.  
  316. inline int operator==(const char* p, const String& s) {
  317.   return strcmp(p, s) == 0;
  318. }
  319.  
  320. inline int operator!=(const char* p, const String& s) {
  321.   return strcmp(p, s) != 0;
  322. }
  323.  
  324. inline int operator<(const char* p, const String& s) {
  325.   return strcmp(p, s) < 0;
  326. }
  327.  
  328. inline int operator>(const char* p, const String& s) {
  329.   return strcmp(p, s) > 0;
  330. }
  331.  
  332. inline int operator<=(const char* p, const String& s) {
  333.   return strcmp(p, s) <= 0;
  334. }
  335.  
  336. inline int operator>=(const char* p, const String& s) {
  337.   return strcmp(p, s) >= 0;
  338. }
  339.  
  340. inline int operator==(const String& s, const char c) {
  341.   return *s == c;
  342. }
  343.  
  344. inline int operator!=(const String& s, const char c) {
  345.   return *s != c;
  346. }
  347.  
  348. inline int operator<(const String& s, const char c) {
  349.   return *s < c;
  350. }
  351.  
  352. inline int operator>(const String& s, const char c) {
  353.   return *s > c;
  354. }
  355.  
  356. inline int operator<=(const String& s, const char c) {
  357.   return *s <= c;
  358. }
  359.  
  360. inline int operator>=(const String& s, const char c) {
  361.   return *s >= c;
  362. }
  363.  
  364. inline int String::Index(const String& t) const {
  365.   return index(*this, t);
  366. }
  367.  
  368. inline int String::Index(const RegExp& t) const {
  369.   return index(*this, t);
  370. }
  371.  
  372. inline String String::SubStr(unsigned p, unsigned n) const {
  373.   return substr(*this, p, n);
  374. }
  375.  
  376. inline int String::Split(String*& a, const String& fs) const {
  377.   return split(*this, a, fs);
  378. }
  379.  
  380. inline int String::Split(String*& a, const RegExp& fs) const {
  381.   return split(*this, a, fs);
  382. }
  383.  
  384. inline int String::Sub(const String& from, const String& to, unsigned count) {
  385.   return gsub(from, to, *this, count);
  386. }
  387.  
  388. inline int String::Sub(const RegExp& from, const String& to, unsigned count) {
  389.   return gsub(from, to, *this, count);
  390. }
  391.  
  392. extern const String STR_NULL;        // a global NULL string
  393. extern const String& StrNull;        // same string, different name
  394. extern const String& Str;        // same string, different name
  395.  
  396. #if defined( __BCOPT__ ) && !defined( _ALLOW_po )
  397. #pragma option -po.
  398. #endif
  399. #pragma option -Vo.
  400.  
  401. #endif
  402.